#include "StatusDisplayWidgetTest.h"
#include "CustomWidget/StatusDisplayWidget.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTimer>
#include <QResizeEvent>
#include <QApplication>
#include <QScreen>

StatusDisplayWidgetTest::StatusDisplayWidgetTest(QWidget *parent) : QWidget(parent), statusDisplayWidget(nullptr)
{
    initUI();

    // 创建模拟数据定时器
    //QTimer *timer = new QTimer(this);
    //connect(timer, &QTimer::timeout, this, [this]() {
    //    // 生成模拟数据
    //    vvl::proto::payload_mcu_data_full data;
    //    auto &resp_40 = data.resp_vvl40;
    //    // 随机生成测试数据
    //    static int counter = 0;
    //    resp_40.depth_m = 5.0 + qSin(counter * 0.1) * 2.0;
    //    resp_40.height_m = 3.0 + qCos(counter * 0.1) * 1.5;
    //    resp_40.compass = fmod(counter * 5.0, 360.0);
    //    resp_40.pitch_dir = (counter % 20) < 10;
    //    resp_40.pitch_value = qSin(counter * 0.05) * 15.0;
    //    resp_40.roll_state = (counter % 30) < 15;
    //    resp_40.roll_value = qCos(counter * 0.05) * 10.0;
    //    resp_40.camera_angle = fmod(counter * 2.0, 180.0);
    //    resp_40.sonar_angle = fmod(counter * 3.0, 360.0);
    //    resp_40.water_temp = 15.0 + qSin(counter * 0.02) * 5.0;
    //    resp_40.cabin_temp = 25.0 + qCos(counter * 0.03) * 3.0;
    //    resp_40.cabin_humidity = 60.0 + qSin(counter * 0.04) * 10.0;
    //    resp_40.water_leak = (counter % 100 == 0) ? 150 : 50;  // 每100次模拟一次漏水
    //    // 更新状态显示
    //    if (statusDisplayWidget)
    //    {
    //        statusDisplayWidget->updateStatusDisplay(data);
    //    }
    //    counter++;
    //});
    //timer->start(500);  // 每500ms更新一次数据
}

void StatusDisplayWidgetTest::initUI()
{
    // 设置窗口属性
    setWindowTitle("状态显示组件测试");
    setMinimumSize(1200, 800);

    // 设置背景图片
    setStyleSheet(
        "StatusDisplayWidgetTest {"
        "background-repeat: no-repeat;"
        "background-position: center;"
        "}");

    // 创建状态显示组件
    statusDisplayWidget = new StatusDisplayWidget(this);
    statusDisplayWidget->setMinimumWidth(1000);

    // 创建标题和说明
    QLabel *titleLabel = new QLabel("状态显示组件测试界面", this);
    titleLabel->setStyleSheet(
        "QLabel {"
        "color: white;"
        "font-size: 24px;"
        "font-weight: bold;"
        "background-color: rgba(0, 0, 0, 120);"
        "padding: 10px;"
        "border-radius: 5px;"
        "}");
    titleLabel->setAlignment(Qt::AlignCenter);

    QLabel *infoLabel = new QLabel(
        "此界面用于测试 StatusDisplayWidget 组件\n"
        "状态栏会显示模拟的机器人传感器数据\n"
        "数据每500ms自动更新一次",
        this);
    infoLabel->setStyleSheet(
        "QLabel {"
        "color: white;"
        "font-size: 14px;"
        "background-color: rgba(0, 0, 0, 120);"
        "padding: 10px;"
        "border-radius: 5px;"
        "}");
    infoLabel->setAlignment(Qt::AlignCenter);

    // 创建控制按钮
    QPushButton *toggleButton = new QPushButton("隐藏/显示状态栏", this);
    toggleButton->setStyleSheet(
        "QPushButton {"
        "background-color: #2E86C1;"
        "color: white;"
        "border: none;"
        "padding: 8px 16px;"
        "border-radius: 4px;"
        "font-size: 14px;"
        "}"
        "QPushButton:hover {"
        "background-color: #2874A6;"
        "}"
        "QPushButton:pressed {"
        "background-color: #1B4F72;"
        "}");

    connect(toggleButton, &QPushButton::clicked, this, [this]() {
        if (statusDisplayWidget)
        {
            bool visible = statusDisplayWidget->isVisible();
            statusDisplayWidget->setVisible(!visible);
        }
    });

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(15);

    // 添加标题和说明
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(infoLabel);
    mainLayout->addStretch();  // 添加弹簧将状态栏推到底部

    // 添加控制按钮（在状态栏上方）
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(toggleButton);
    buttonLayout->addStretch();
    mainLayout->addLayout(buttonLayout);

    // 状态栏的位置将在resizeEvent中设置
}

void StatusDisplayWidgetTest::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    updateStatusDisplayPosition();
}

void StatusDisplayWidgetTest::updateStatusDisplayPosition()
{
    if (!statusDisplayWidget)
        return;

    // 获取状态栏的推荐尺寸
    QSize statusSize = statusDisplayWidget->sizeHint();
    if (statusSize.isEmpty())
    {
        statusSize = QSize(800, 60);  // 使用默认尺寸
    }

    // 计算位置：水平居中，距离底部20像素
    int x = 0;
    int y = height() - statusSize.height() - 20;
    // 确保位置在可见范围内
    x = qMax(10, x);
    y = qMax(10, y);

    statusDisplayWidget->setGeometry(x, y, statusSize.width(), statusSize.height());
    statusDisplayWidget->raise();  // 确保在最上层
}